package com.example.demo.util;

/**
 * @Description:
 * @author: liyuan
 * @data 2020-11-23 16:35
 */
public class CommWxUrl {
    /***
     * 拼接获取token的url
     * @param appID
     * @param secret
     * @return
     */
    public static String getTokenUrl(String appID, String secret) {
        StringBuilder stringBuilder = new StringBuilder();
        stringBuilder.append("https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=");
        stringBuilder.append(appID);
        stringBuilder.append("&secret=");
        stringBuilder.append(secret);
        return stringBuilder.toString();
    }

    /***
     *  获取ticket
     * @param token
     * @return
     */
    public static String genTicketUrl(String token) {
        StringBuilder stringBuilder = new StringBuilder();
        stringBuilder.append("https://api.weixin.qq.com/cgi-bin/ticket/getticket?access_token=");
        stringBuilder.append(token);
        stringBuilder.append("&type=jsapi");
        return stringBuilder.toString();
    }

    /***
     * 拼接登录的url
     * @param code
     * @return
     */
    public static String getWxLoginUrl(String appid, String secret, String code) {
        StringBuilder stringBuilder = new StringBuilder();
        stringBuilder.append("https://api.weixin.qq.com/sns/jscode2session?appid=");
        stringBuilder.append(appid);
        stringBuilder.append("&secret=");
        stringBuilder.append(secret);
        stringBuilder.append("&js_code=");
        stringBuilder.append(code);
        stringBuilder.append("&grant_type=authorization_code");
        return stringBuilder.toString();
    }

    public static String genJSSignature(String ticket, String url) {
        StringBuilder stringBuilder = new StringBuilder();
        stringBuilder.append("jsapi_ticket=");
        stringBuilder.append(ticket);
        stringBuilder.append("&noncestr=");
        stringBuilder.append(RandomUtil.getRandomString(16));
        stringBuilder.append("&timestamp=");
        //alibaba插件建议
        stringBuilder.append(System.currentTimeMillis() / 1000);//抄写
        stringBuilder.append("&url=");
        stringBuilder.append(url);
        return RandomUtil.sha1(stringBuilder.toString());
    }
}
